//SYSC3303-assignment#1
//Student Name: Qi Zhang
//Student No. 100839438
//Date:12,July,2011

import java.io.*;
import java.net.*;

public class Client {

	DatagramSocket sendReceiveSocket;
	DatagramPacket sendPacket, receivePacket;
    byte a=1;
    
   public Client()
   {
      try {
         // Construct a datagram socket and bind it to 5000
         // port on the local host machine. This socket will be used to
         // send and receive UDP Datagram packets.
         sendReceiveSocket = new DatagramSocket(5000);
      } catch (SocketException e) {   // Can't create the socket.
         e.printStackTrace();
         System.exit(1);
      }
   }

   public void sendAndReceive()
   {
      //*Send Part* Prepare a DatagramPacket and send it via sendReceiveSocket
      // to port 68 on the intermediate host.
	  
	   if(a==1){    //"read request" or "write request"
		   a++;
		}
	   else{
		   a--;
	   }
	   
	   String str1=new String ("test.txt");
	   byte [] msg1= str1.getBytes();
	
	   String str2= new String ("octEt");
	   byte [] msg2= str2.getBytes();
	    
	   byte [] msg= new byte[17];   
	   msg[0]=0;
	   msg[1]=a; //distinguish read/write request
	   System.arraycopy(msg1,0,msg,2,msg1.length);
	   msg[msg1.length+2]=0;
	   System.arraycopy(msg2,0,msg,msg1.length+3,msg2.length);
	   msg[msg1.length+3+msg2.length]=0;
	   
	   String s= new String(msg);
	  
	   System.out.println("Client: sending a packet containing (string): " + s );
	   System.out.println("Client: sending a packet containing(bytes): " + msg);
     
      try {
         sendPacket = new DatagramPacket(msg, msg.length,
                                         InetAddress.getLocalHost(), 68);
      } catch (UnknownHostException e) {
         e.printStackTrace();
         System.exit(1);
      }

     // System.out.println("To host: " + sendPacket.getAddress());
      //System.out.println("Intermediate host port: " + sendPacket.getPort());
   

      // Send the datagram packet to the intermediate host via the send/receive socket. 

      try {
         sendReceiveSocket.send(sendPacket);
      } catch (IOException e) {
         e.printStackTrace();
         System.exit(1);
      }

      System.out.println("Client: Packet sent.");

  
      //*Receive Part* Construct a DatagramPacket for receiving packets 

      byte data[] = new byte[4];   //the length of send back bytes up to 4
      receivePacket = new DatagramPacket(data, data.length);

      try {
         // Block until a datagram is received via sendReceiveSocket.  
         sendReceiveSocket.receive(receivePacket);
      } catch(IOException e) {
         e.printStackTrace();
         System.exit(1);
      }

      // Process the received datagram.
      System.out.println("Client: Packet received:");
      
      // Get a reference to the data inside the received datagram.
      data = receivePacket.getData(); 
      System.out.println("Containing: (bytes) "+data);
      System.out.println("---------------------------");
      
      //sendReceiveSocket.close();
   }

   public static void main(String args[])
   {
      Client c = new Client();
      for (int i=0;i<10;i++){
          c.sendAndReceive();
      }
   }
}